//
//  FeedbackViewController.m
//  e-nurse
//
//  Created by  on 12/18/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FeedbackViewController.h"
#import "UIPlaceHolderTextView.h"
#import "ServerProxy.h"
#import "ALToastView.h"

@interface FeedbackViewController ()

-(void)feedbackCompleted:(NSNumber*)success;
-(void)feedbackCompletedImpl:(NSNumber*)success;
@end

@implementation FeedbackViewController
@synthesize textView = _textView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"SETTING_FEEDBACK", nil);
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.textView.placeholder = NSLocalizedString(@"FEEDBACK_PLACEHOLDER", nil);
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.textView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction)closeButtonClicked:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(IBAction)submitButtonClicked:(id)sender
{
    [self.textView resignFirstResponder];
    if([self.textView.text isEqualToString:@""])
    {
        [ALToastView toastInView:self.view withText:NSLocalizedString(@"FEEDBACK_CANT_EMPTY", nil)];
        return;
    }
    
    [[ServerProxy sharedInstance] feedback:self.textView.text completedAction:@selector(feedbackCompleted:) target:self];
}

-(void)feedbackCompleted:(NSNumber*)success
{
    [success retain];
    [self performSelectorOnMainThread:@selector(feedbackCompletedImpl:) withObject:success waitUntilDone:NO];
}

-(void)feedbackCompletedImpl:(NSNumber*)success
{
    if([success boolValue])
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
       [ALToastView toastInView:self.view withText:NSLocalizedString(@"FEEDBACK_SEND_FAILED", nil)]; 
    }
    [success release];
}
@end
